package com.webserver.controller;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Article;
import com.webserver.vo.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ArticleController {
    //保存文章的目录名字
    private static String articleDirName = "./articles/";

    static {
        //程序加载时判断目录是否存在，不存在则自动创建
        File articleDir = new File(articleDirName);
        if (!articleDir.exists()) {
            articleDir.mkdirs();
        }
    }

    public void writeArticle(HttpRequest request, HttpResponse response) {
        System.out.println("正在保存文章...");
        String title = request.getParameters("title");
        String content=request.getParameters("content");
        String author = request.getParameters("author");
        System.out.println(title + "," + content);

        //判断值是否为空
        if (title == null || content == null || author == null) {
            response.setEntity(new File("./webapps/myweb/article_fail.html"));
            return;
        }

        Article article = new Article(title, content, author);
        try (
                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream(articleDirName + title + ".obj")
                );
        ) {
            oos.writeObject(article);
            //保存成功
            response.setEntity(new File("./webapps/myweb/article_success.html"));
            System.out.println("文章保存成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void showAllArticle(HttpRequest request, HttpResponse response) {
        System.out.println("开始生成动态页面...");
        List<Article> articlesList = new ArrayList<>();
        File articleDir = new File(articleDirName);
        File[] subs = articleDir.listFiles(file -> file.getName().endsWith(".obj"));
        for (File file : subs) {
            try (
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(file)
                    );
            ) {
                articlesList.add((Article) ois.readObject());

            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        articlesList.forEach(System.out::println);

        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>\n");
        pw.println("<html lang=\"en\">\n");
        pw.println("<head>\n");
        pw.println("    <meta charset=\"UTF-8\">\n");
        pw.println("    <title>文章列表</title>\n");
        pw.println("</head>\n");
        pw.println("<body>\n");
        pw.println("    <center>\n");
        pw.println("        <h1>文章列表</h1>\n");
        pw.println("        <table border=\"1\">\n");
        pw.println("</html>");
        pw.println("            <tr>\n");
        pw.println("                <td>标题</td>\n");
        pw.println("                <td>作者</td>\n");
        pw.println("                <td>正文</td>\n");
//        pw.println("                <td>年龄</td>\n");
        pw.println("            </tr>\n");

        for (Article article : articlesList) {
            pw.println("            <tr>\n");
            pw.println("<td>" + article.getTitle() + "</td>");
            pw.println("<td>" + article.getAuthor() + "</td>");
            pw.println("<td>" + article.getContent() + "</td>");
//            pw.println("<td>" + user.getAge() + "</td>");
            pw.println("            </tr>\n");
        }
        pw.println("        </table>\n");
        pw.println("    </center>\n");
        pw.println("</body>\n");
        //设置正文类型
        response.setContentType("text/html");



        System.out.println("动态页面生成完成！");

    }
}














